package com.tanhua.dubbo.huanxin.advice;


import cn.hutool.core.util.StrUtil;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class RecoverAdvice {

    @Autowired
    private AliyunSMSConfig aliyunSMSConfig;

    @Pointcut("execution(* com.tanhua.dubbo.huanxin.service.RequestService.recover(..))")
    public void executeService(){
    }

    @After("executeService()")
    public void returnAfter(JoinPoint joinPoint){
        System.out.println("AOP after");

        String code = "重试次数完毕！不能注册";

        SendSmsResponse sendSmsResponse;
        try {
            Config config = new Config()
                    .setAccessKeyId(this.aliyunSMSConfig.getAccessKeyId())
                    .setAccessKeySecret(this.aliyunSMSConfig.getAccessKeySecret())
                    .setEndpoint(this.aliyunSMSConfig.getDomain());
            // 访问的域名
            Client client = new Client(config);

            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers("15936999825") //目标手机号
                    .setSignName(this.aliyunSMSConfig.getSignName()) //签名名称
                    .setTemplateCode(this.aliyunSMSConfig.getTemplateCode()) //短信模板code
                    .setTemplateParam("{\"code\":\"" + code + "\"}");

            //发送请求
            sendSmsResponse = client.sendSms(sendSmsRequest);

            if (StrUtil.equals(sendSmsResponse.getBody().getCode(), "OK")){
                System.out.println("sendSmsRequest = " + sendSmsRequest);
            }

        } catch (Exception e) {
            log.error("发送短信验证码失败！", e);
        }

    }

}
